﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_EditUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FetchDistrict();

            string uid = Request.Params["uid"];
            if (uid != null)
            {
                CmsDataContext db = new CmsDataContext();

                #region 获取用户信息
                Manager manager = (from aM in db.Manager
                                   where aM.ID == uid
                                   select aM).First();

                UserID.Text = manager.ID;
                UserID.ReadOnly = true;
                UserName.Text = manager.Name;
                District.SelectedValue = manager.District;

                FetchDepartment();

                Belong.SelectedValue = manager.Belong;
                #endregion

                #region 处理权限
                var q = from aP in db.Power
                         where aP.ManagerID == uid
                         select aP;
                foreach (Power p in q)
                {
                    switch (p.Authority)
                    {
                        case (int)Powers.ArticleManager:
                            ArticleManager.Checked = true;
                            break;
                        case (int)Powers.ArticleVerifier:
                            ArticleVerifier.Checked = true;
                            break;
                        case (int)Powers.SystemManager:
                            SystemManager.Checked = true;
                            break;
                        case (int)Powers.TeacherManager:
                            TeacherManager.Checked = true;
                            break;
                        case (int)Powers.TeacherVerifier:
                            TeacherVerifier.Checked = true;
                            break;
                        case (int)Powers.TrainManager:
                            TrainManager.Checked = true;
                            break;
                        case (int)Powers.Statistician:
                            Statistician.Checked = true;
                            break;
                    }
                }
                #endregion
            }
        }
    }

    private void FetchDistrict()
    {
        District.Items.Clear();

        XmlDataSource xds1 = new XmlDataSource();
        xds1.DataFile = Toolkit.GetRealDataFilePath("Department.xml");
        District.DataSource = xds1;

        District.DataMember = "//District";
        District.DataTextField = "name";
        District.DataValueField = "name";
        District.DataBind();

        District.Items.Insert(0, new ListItem("请选择", ""));

        Belong.Items.Clear();
        Belong.Items.Insert(0, new ListItem("请选择", ""));
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        string uid = UserID.Text.Trim();
        string uname = UserName.Text.Trim();

        CmsDataContext db = new CmsDataContext();

        try
        {
            Manager m;
            var q = from aM in db.Manager where aM.ID == uid select aM;

            #region 先处理用户信息
            if (UserID.ReadOnly)
            {
                //更新用户信息
                m = q.First();
            }
            else
            {
                //新增用户信息，先看看数据有没有数据
                if (q.Count() > 0)
                {
                    MessageBox.Message = "用户名已经存在。";
                    return;
                }

                m = new Manager();
                m.ID = uid;
                m.Password = Toolkit.EncryptPassword(Toolkit.InitialPassword);

                db.Manager.InsertOnSubmit(m);
            }

            m.Name = uname;
            m.District = District.SelectedValue;
            m.Belong = Belong.SelectedValue;
            #endregion

            #region 删除用户已经的权限
            var q2 = from aP in db.Power
                     where aP.ManagerID == uid
                     select aP;
            db.Power.DeleteAllOnSubmit(q2);
            #endregion

            #region 应用本次设置的权限
            if (ArticleManager.Checked)
            {
                Power p = new Power();
                p.Authority = (int)Powers.ArticleManager;
                p.Description = "文章管理员";
                p.ID = Toolkit.NextUUID;
                p.ManagerID = uid;

                db.Power.InsertOnSubmit(p);
            }

            if (ArticleVerifier.Checked)
            {
                Power p = new Power();
                p.Authority = (int)Powers.ArticleVerifier;
                p.Description = "文章审核员";
                p.ID = Toolkit.NextUUID;
                p.ManagerID = uid;

                db.Power.InsertOnSubmit(p);
            }

            if (TeacherManager.Checked)
            {
                Power p = new Power();
                p.Authority = (int)Powers.TeacherManager;
                p.Description = "教师信息管理员";
                p.ID = Toolkit.NextUUID;
                p.ManagerID = uid;

                db.Power.InsertOnSubmit(p);
            }

            if (TeacherVerifier.Checked)
            {
                Power p = new Power();
                p.Authority = (int)Powers.TeacherVerifier;
                p.Description = "教师信息审核员";
                p.ID = Toolkit.NextUUID;
                p.ManagerID = uid;

                db.Power.InsertOnSubmit(p);
            }

            if (TrainManager.Checked)
            {
                Power p = new Power();
                p.Authority = (int)Powers.TrainManager;
                p.Description = "培训管理员";
                p.ID = Toolkit.NextUUID;
                p.ManagerID = uid;

                db.Power.InsertOnSubmit(p);
            }

            if (SystemManager.Checked)
            {
                Power p = new Power();
                p.Authority = (int)Powers.SystemManager;
                p.Description = "系统管理员";
                p.ID = Toolkit.NextUUID;
                p.ManagerID = uid;

                db.Power.InsertOnSubmit(p);
            }

            if (Statistician.Checked)
            {
                Power p = new Power();
                p.Authority = (int)Powers.Statistician;
                p.Description = "统计员";
                p.ID = Toolkit.NextUUID;
                p.ManagerID = uid;

                db.Power.InsertOnSubmit(p);
            }
            #endregion

            db.SubmitChanges();

            MessageBox.Message = "用户信息保存成功。";
            MessageBox.Location = "./EditUser.aspx?uid=" + uid;
        }
        catch
        {
            MessageBox.Message = "保存用户信息时发生错误。";
        }
    }
    protected void District_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchDepartment();
    }

    private void FetchDepartment()
    {
        Belong.Items.Clear();

        XmlDataSource xds2 = new XmlDataSource();
        xds2.DataFile = Toolkit.GetRealDataFilePath("Department.xml");
        xds2.XPath = string.Format("/Root/District[@name='{0}']//Depart", District.SelectedValue);
        Belong.DataSource = xds2;
        Belong.DataMember = "Depart";
        Belong.DataTextField = "name";
        Belong.DataValueField = "name";
        Belong.DataBind();

        Belong.Items.Insert(0, new ListItem("请选择", ""));
    }
}